*	Replicate results in Painter (2020) using his data.
*	Mike Schwert 8/10/21

clear all
set more off

set scheme paul
set matsize 5000


use "$dataPath/Painter/muni_climate_rep_data.dta"

gen issue_size = exp(lnsize)/1000000
gen num_cusips = exp(lndeal)
gen num_underwriter = exp(lnnum_undwrt)

gen year = substr(stateyr,3,6)
destring year, replace

gen state = substr(stateyr,1,2)
egen state_id = group(state)
egen county_id = group(county)

* Table 2 - summary statistics
eststo clear

estpost sum tot_cost wyield wspread issue_size max_maturity callable insurance rating sinkable GO prerefunded competitive AMT fed_ex state_ex num_cusips num_underwriter if malgdp > 0
est store a

estpost sum tot_cost wyield wspread issue_size max_maturity callable insurance rating sinkable GO prerefunded competitive AMT fed_ex state_ex num_cusips num_underwriter if malgdp == 0
est store b

esttab a b using "$tablePath/Painter_summary.tex", booktabs replace compress nogaps b(3) /// ///
mtitles("\textbf{\emph{Climate Bonds}}" "\textbf{\emph{Non-Climate Bonds}}") ///
collabels(\multicolumn{1}{c}{{Mean}} \multicolumn{1}{c}{{Std.Dev.}} \multicolumn{1}{l}{{Obs.}}) ///
cells("mean(fmt(2)) sd(fmt(2)) count(fmt(%9.0fc))") label

* Table 3 - main regression analysis

est clear
global controls callable insurance sinkable GO prerefunded competitive fed_ex state_ex AMT lndeal lnnum_undwrt

eststo: reghdfe tot_cost malgdp lnsize lnmaturity rating if max_maturity >= 25, absorb(state_id##year) vce(cluster county_id)
estadd local state_year_fe "Y"
eststo: reghdfe tot_cost malgdp lnsize lnmaturity rating $controls if max_maturity >= 25, absorb(state_id##year) vce(cluster county_id)
estadd local state_year_fe "Y"
eststo: reghdfe tot_cost lnmalgdp lnsize lnmaturity rating $controls if max_maturity >= 25, absorb(state_id##year) vce(cluster county_id)
estadd local state_year_fe "Y"

eststo: reghdfe tot_cost malgdp lnsize lnmaturity rating if max_maturity < 25, absorb(state_id##year) vce(cluster county_id)
estadd local state_year_fe "Y"
eststo: reghdfe tot_cost malgdp lnsize lnmaturity rating $controls if max_maturity < 25, absorb(state_id##year) vce(cluster county_id)
estadd local state_year_fe "Y"
eststo: reghdfe tot_cost lnmalgdp lnsize lnmaturity rating $controls if max_maturity < 25, absorb(state_id##year) vce(cluster county_id)
estadd local state_year_fe "Y"

esttab, compress nogaps b(3) stats(state_year_fe N r2, fmt(%9.3fc %9.0fc %9.3fc) labels("State-Year F.E." "Observations" "R-Squared")) label starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles 
esttab using $tablePath/Painter_regs.tex, booktabs replace compress nogaps b(3) stats(state_year_fe N r2, fmt(%9.3fc %9.0fc %9.3fc) labels("State-Year F.E." "Observations" "R-Squared")) label starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles 

est clear

* Appendix Figure - plot the climate risk effect over time
* exclude 2004 due to extremely wide confidence band

global controls lnsize lnmaturity rating callable insurance sinkable GO prerefunded competitive fed_ex state_ex AMT lndeal lnnum_undwrt

reghdfe tot_cost i.year#c.malgdp lnsize lnmaturity rating $controls if max_maturity >= 25, absorb(state_id##year) vce(cluster county_id)
mat b =e(b)
mat b = b[1,2..14]
mat b = b'
mat V =e(V)
mat V = vecdiag(V)
mat V = V[1,2..14]'
preserve
clear
svmat b
svmat V
gen year = 2004 + _n
gen ci_upper = b1 + sqrt(V1)*1.96
gen ci_lower = b1 - sqrt(V1)*1.96 
twoway (rcap ci_upper ci_lower year) (scatter b1 year), yline(0, lcolor(black)) xlabel(2005(2)2017) xtitle("Year") legend(off) graphregion(color(white))
graph export "$graphPath/Painter_annual_over25.png", replace
restore

reghdfe tot_cost i.year#c.malgdp lnsize lnmaturity rating $controls if max_maturity < 25, absorb(state_id##year) vce(cluster county_id)
mat b =e(b)
mat b = b[1,2..14]
mat b = b'
mat V =e(V)
mat V = vecdiag(V)
mat V = V[1,2..14]'
preserve
clear
svmat b
svmat V
gen year = 2004 + _n
gen ci_upper = b1 + sqrt(V1)*1.96
gen ci_lower = b1 - sqrt(V1)*1.96 
twoway (rcap ci_upper ci_lower year) (scatter b1 year), yline(0, lcolor(black)) xlabel(2005(2)2017) xtitle("Year") legend(off) graphregion(color(white))
graph export "$graphPath/Painter_annual_under25.png", replace
restore


